/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *   g.latarowski@gmail.com                                                *
 *   maciej.gajewski0@gmail.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


// local
#include "gbooldelegate.h"



// ===========================================================================
//                              GBoolDelegate
// ===========================================================================


// --------------- GBoolDelegate::GBoolDelegate() ----------------------------
GBoolDelegate::GBoolDelegate( QObject * pParent )
    : GItemDelegate( pParent )
{
	_values.insert( tr( "Yes" ), QVariant( true ) );
	_values.insert( tr( "No" ), QVariant( false ) );
}
// ---------------------------------------------------------------------------


// --------------------- GBoolDelegate::displayText() ------------------------
QString GBoolDelegate::displayText( const QVariant & value )const
{
	return _values.key( value );
}
// ---------------------------------------------------------------------------


// --------------- GBoolDelegate::createEditor() -----------------------------
QWidget * GBoolDelegate::createEditor( QWidget * pParent,
									   const QStyleOptionViewItem & option,
									   const QModelIndex & index
									 ) const
{
	Q_UNUSED( option );
	Q_UNUSED( index );
	
	// create and configure editor
	
	QComboBox * pEditor = new QComboBox( pParent );
	if ( pEditor )
	{
			// set texts
		pEditor->addItems( _values.keys() );
			// make read-only
		pEditor->setEditable( false );
	}
	
	connect( pEditor, SIGNAL( activated( int ) ), SLOT( comboboxChanged() ) );
	
    return pEditor;
}
// ---------------------------------------------------------------------------


// --------------- GBoolDelegate::setEditorData() ----------------------------
/// Initializes editor widhet with proper data
void GBoolDelegate::setEditorData( QWidget * pEditor,
								   const QModelIndex & index
								 ) const
{
	Q_ASSERT( index.model() );
	
	QVariant value = index.model()->data( index, Qt::UserRole );
	
	QComboBox * pComboBox = qobject_cast< QComboBox * >( pEditor );
	
	if( pComboBox )
	{
		// display proper item - assumpiton - map has unique values
				
			// find text for value
		QString text = _values.key( value );
			// find index of the text
		int index = _values.keys().indexOf( text );
			// set the index
		pComboBox->setCurrentIndex( index );
	}
}
// ---------------------------------------------------------------------------


// --------------- GBoolDelegate::setModelData() -----------------------------
void GBoolDelegate::setModelData( QWidget * pEditor,
								  QAbstractItemModel * pModel,
		  						  const QModelIndex & index
								) const
{
    QComboBox * pComboBox = qobject_cast< QComboBox * >( pEditor );
	if( pComboBox )
	{
		QString name = pComboBox->currentText();
		QVariant value = _values[ name ];
		pModel->setData( index, value, Qt::UserRole );
		pModel->setData( index, name, Qt::DisplayRole );
	}
}
// ---------------------------------------------------------------------------
/// Commits data to model on each combo box change. Without this, data would be commited on leaving editor
void GBoolDelegate::comboboxChanged()
{
	
	QComboBox* pWidget = qobject_cast<QComboBox*>( sender() );
	if ( pWidget )
	{
		// force data commit on each value change. Don't wait for widget to lost focus
		emit commitData( pWidget );
	}
}





// eof
